//
//  VisitorView.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/9/27.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit
import SnapKit

@objc
protocol VisitorViewDelegate: NSObjectProtocol
{
    optional func visitorViewDidClickLoginBtn(visitorView: VisitorView)
    optional func visitorViewDidClickRegisterBtn(visitorView: VisitorView)
}


class VisitorView: UIView {
    
    weak var delegate: VisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(red: 232/255.0, green: 233/255.0, blue: 232/255.0, alpha: 1.0)
        
        setUpUI()
    }
    
    func setupVisitorInfo(isHome: Bool, imageName: String, info: String)
    {
        icon.hidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        message.text = info
        
        if isHome
        {
            startAnimator()
        }
    }
    
    func startAnimator()
    {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        anim.removedOnCompletion = false
        icon.layer.addAnimation(anim, forKey: nil)
    }
    
    
    private func setUpUI()
    {
        addSubview(icon)
        addSubview(maskBgView)
        addSubview(homeIcon)
        addSubview(message)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 布局子控件
        icon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        homeIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        message.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(icon.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(self)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(message.snp_bottom).offset(20)
            make.left.equalTo(message.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 2.5布局登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(message.snp_bottom).offset(20)
            make.right.equalTo(message.snp_right)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 2.5布局背景图片
        maskBgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }
    }
    
    // MARK: - 懒加载
    /// 转盘
    private lazy var icon: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        }()
    /// 图标
    private lazy var homeIcon: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        }()
    /// 文本
    private lazy var message: UILabel = {
        let label = UILabel()
        label.text = "附近的思考了几分离开的时间发来看电视剧罚款了但是但是"
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        return label
        }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 注册按钮点击监听
        btn.addTarget(self, action: Selector("registerClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 注册按钮点击监听
        btn.addTarget(self, action: Selector("loginClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    /// 背景图片
    private lazy var maskBgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
        }()
    
    
    func registerClick()
    {
        delegate?.visitorViewDidClickRegisterBtn!(self)
    }
    
    func loginClick()
    {
        delegate?.visitorViewDidClickLoginBtn!(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
